home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagd_f.zip / FAQ.SWG / 0008_BORLAND - OOP QA.pas < prev    next >
Pascal/Delphi Source File  |  1993-06-01  |  7KB  |  214 lines

  1.  
  2. TP 5.5 OOP - OBJECT EXE FILE SIZE OVERHEAD
  3. Q. How much overhead will result in the *.EXE file from using the
  4.    object oriented style?
  5. A. The overhead will result from the pointer from the object to
  6.    its method.  This is a 4 byte pointer, so there isn't that
  7.    much extra code generated.
  8.  
  9.  
  10. TP 5.5 OOP - PROTECTED AND PRIVATE FIELDS
  11. Q. Does Turbo Pascal 5.5 support Protected or Private fields?
  12. A. No it does not.
  13.  
  14.  
  15. TP 5.5 OOP - RECORDS VS. OBJECTS
  16. Q. What things can be done with Records that can not be done with
  17.    Objects?
  18. A. You cannot have:
  19.  
  20.      1. Variant Objects.
  21.      2. Objects with absolutes.
  22.      3. Directly nested Objects.
  23.  
  24.    You can have:
  25.  
  26.        1. Pointers to objects.
  27.  
  28. TP 5.5 OOP - EXTERNAL METHODS
  29. Q. Can methods within an object be external?
  30. A. Yes. Virtual and Static methods can be written as external
  31.    code. There is no difference between an external Virtual or
  32.    Static method. External Constructors and Destructors are
  33.    difficult to write due to the Prolog and Epilog code within
  34.    them.
  35.  
  36. TP 5.5 OOP - CONSTRUCTOR USE
  37. Q. What are the three purposes of a Constructor?
  38. A. 1. Insert the address of the VMT into the Object variable.
  39.         (Implicit)
  40.    2. To allocate memory for the Object variable. 
  41.         (Implicit)
  42.    3. To initialize the Object variable. 
  43.         (Explicit)
  44.  
  45. TP 5.5 OOP - DESTRUCTOR HEAP CORRUPTION
  46. Q. Why is my destructor fragging my heap?
  47. A. Use: 
  48.      
  49.      Dispose(ptr,done);
  50.  
  51.    instead of:
  52.    
  53.      ptr^.done;
  54.      Dispose(done);
  55.  
  56. TP 5.5 OOP - SAVING OBJECTS TO DISK
  57. Q. Can I save my objects to a disk file like I can a record
  58.    structure?
  59. A. We have provided an example program on the distribution
  60.    diskette, STREAMS, which documents how to save an object to
  61.    disk.
  62.  
  63. TP 5.5 OOP - FILES OF OBJECT TYPE
  64. Q. Why can't I make a file of ObjectType?
  65. A. Because by the rules of polymorphism, any descendant of
  66.    ObjectType would be type compatible and be able to be written 
  67.    to disk as well. The problem with this is that the descendants
  68.    may be (and usually are) of a larger size than the ObjectType 
  69.    itself.  Pascal's file structure require all records to be 
  70.    the same size, otherwise, how do you know which size object 
  71.    do you read in? For an example on how to do Object disk I/O, 
  72.    please see the STREAMS example on the distribution diskettes.
  73.  
  74. TP 5.5 OOP - SIZEOF OBJECTS CONSTRUCTOR
  75. Q. Why does Sizeof(MyObject) return a size 2 bytes larger than I
  76.    expect when no virtual methods are used?
  77. A. By placing a constructor in your object, you're making the 
  78.    object virtual.
  79.  
  80. TP 5.5 OOP - LINKER STRIPS UNUSED STATIC METHODS
  81. Q. It appears that my static methods are getting stripped from 
  82.    my program by the smart linker when they are unused. Yet, 
  83.    my virtual methods are not. How come?
  84. A. Static methods can be stripped because it can be determined at
  85.    link time what methods will be called. Virtual methods can 
  86.    not be stripped because the program will not know what methods
  87.    will be used, and what will not, until run time. This is 
  88.    because of late binding.
  89.  
  90. TP 5.5 OOP - CONSTRUCTOR CALL TO ANCESTOR WITH VIRTUALS
  91. Q. If a descendant of a virtual object defines no virtual
  92.    methods of its own, does it need to call the ancestor's
  93.    constructor?
  94. A. If an object is a descendant of a virtual object, it must call
  95.    that ancestor's constructor or it's own constructor. Even if
  96.    the new object does not define any virtuals of its own. For
  97.    example:
  98.  
  99.      Type
  100.        A = Object 
  101.              Constructor Init; 
  102.              Procedure AA; Virtual; 
  103.        End; 
  104.        B = Object ( A ) 
  105.        End; 
  106.  
  107.    For each instance of A and of B, Init must be called or the
  108.    Virtual Method Table pointer will not be loaded correctly.
  109.  
  110. TP 5.5 OOP - OVERRIDE VIRTUAL METHOD CALLING ANCESTOR
  111. Q. Can I override a virtual method and force a call to the
  112.    ancestor objects method?
  113. A. No. Late binding will always call the current method and it
  114.    defeats the purpose of object oriented program to go around 
  115.    this feature.
  116.  
  117. TP 5.5 OOP - CONSTRUCTOR CALL WITHIN METHOD
  118. Q. I am calling my constructor from within a method, why am I
  119.    having problems?
  120. A. The problem will arise when the constructor loads the new VMT
  121.    pointer. It loads the pointer to the VMT for the constructor's
  122.    table, not the instances. Therefore if a descendant calls an
  123.    ancestor's constructor, the descendant's VMT will now point to
  124.    the ancestors VMT. The problem now occurs when the descendant
  125.    tries to call a method that was defined after the ancestor.
  126.    The VMT entry for this method is unknown. Look at the
  127.    following example: 
  128.  
  129.      Type 
  130.        L1 = Object 
  131.               Constructor Init;
  132.               Procedure First; Virtual; 
  133.        End; 
  134.        L2 = Object ( L1 ); 
  135.               Constructor Init; 
  136.               Procedure Second; Virtual; 
  137.        End;
  138.  
  139.      Constructor L1.Init;
  140.      Begin
  141.      End;
  142.  
  143.      Constructor L2.Init;
  144.      Begin
  145.      End;
  146.  
  147.      Procedure L1.First;
  148.      Begin
  149.        Init;
  150.      End;
  151.  
  152.      Procedure L2.Second;
  153.      Begin
  154.        Init;
  155.      End;
  156.  
  157.      Var
  158.        L : L2;
  159.  
  160.      Begin
  161.        L.Init;   { This calls L2.Init and loads a pointer to }
  162.                  { the L2 VMT into L.                        }
  163.        L.First;  { This will call L1.First, which in turn calls }
  164.                  { L1.Init because as far as the procedure is   }
  165.                  { concerned, the Self pointer is a pointer     }
  166.                  { to an object of type L1.                     }
  167.        L.Second; { This is undefined. Since the VMT now       }
  168.                  { pointed to by L is L1's, the pointer to    } 
  169.                  { method Second is undefined. Therefore, the }
  170.                  { call to this method is undefined.          }
  171.        ...
  172.  
  173. TP 5.5 OOP - CONSTRUCTOR CALL WITHIN POLYMORPHIC METHOD
  174. Q. Does the previous question apply to polymorphic procedures?
  175. A. Yes. The previous question and answer apply to every case
  176.    where the compiler may think of an object as its ancestor. A
  177.    polymorphic example that is incorrect follows:
  178.  
  179.      Procedure Init ( var x : L1 );
  180.      Begin
  181.        x.Init;  { This will ALWAYS call L1.Init }
  182.      End;
  183.  
  184. TP 5.5 OOP - CONSTRUCTOR CALLING ANCESTOR'S CONSTRUCTOR
  185. Q. Should my current object's constructor call it's ancestor's
  186.    constructors?
  187. A. As a rule of thumb, this is the correct thing to do. It is
  188.    okay to not do it, but it does allow initialization of
  189.    whatever the previous constructors did.
  190.  
  191. TP 5.5 OOP - CALLING ANCESTORS CONSTRUCTOR
  192. Q. How do you call a constructor from an ancestor's object?
  193. A. You can call an ancestor's constructor directly from within
  194.    the constructor of the current object.  For example:
  195.  
  196.      Type
  197.        Type1 = Object
  198.                  Constructor Init;
  199.        End;
  200.        Type2 = Object ( Type1 )
  201.                  Constructor Init;
  202.        End;
  203.  
  204.      Constructor Type1.Init;
  205.      Begin
  206.      End;
  207.  
  208.      Constructor Type2.Init;
  209.      Begin
  210.        Type1.Init;
  211.      End;
  212.      ...
  213.  
  214.